Skip to content

Cover same-bar market pyramid adds per TV's open-tick fill priority#97

Merged
luisleo526 merged 1 commit into
mainfrom
fix/ki62-collision-coverage
Jul 11, 2026
Merged

Cover same-bar market pyramid adds per TV's open-tick fill priority#97
luisleo526 merged 1 commit into
mainfrom
fix/ki62-collision-coverage

Conversation

@luisleo526

Copy link
Copy Markdown
Collaborator

Summary

Implements the probe-pinned KI-62 keep/scratch rule: when a pre-queued same-ID market add and its from_entry bracket exit both fill on the same bar, the exit covers the add (dur-0 scratch) iff the add fills at-or-before the exit in TV's fill sequence. Intrabar exit fills always cover; at the open tick the priority is buy market-like → sell market-like → gapped-through limits, so the only keep cell is a short position gapped at-or-through its buy stop.

Rule pinned at 100% event-level on two dedicated TV probes (9,504/9,504 collision events) and reproduces the masayanfx exemplar's full collision census (4,190/4,190, incl. both hand-verified exemplars).

Changes

  • sort_orders_by_fill_phase (src/engine_fills.cpp): three-tier open-tick priority for a market add paired with its own gapped from_entry bracket; every other pair keeps the existing order.
  • cover_samebar_market_adds_on_exit (src/engine_orders.cpp, called from apply_exit_order_fill): scratches surviving same-bar market-add slices of the exiting ID at the exit's fill price after the frozen pre-add reserve drains; prior-bar slices untouched. New PyramidEntry.market_pyramid_add flag, propagated through fifo_drain.
  • New test suite test_same_bar_add_exit_coverage (9 cells / 30 checks; 21 RED on base).

Verification

  • ctest 95/95; REDs stash-cycle proof independently reproduced by fresh-context review (21 FAIL on base → 30/30 with fix).
  • Corpus: 252 ok, tiers 240/11/1 (baseline held).
  • masayanfx-scalping: 83.8% → 100.0% (10,105/10,105, px 100%, canonical P90s 0.0).
  • Keep/scratch probes: 9,504/9,504 both (dur-0 censuses equal TV exactly).
  • Byte-identical sentinels: lukeborgerding, 3commas-bch-heikin, waranyutrkm pair, ahtisham, ki65/coof probes, coof cohort, 3commas DCA/grid set.
  • Full 412-slug private sweep: 1 up / 0 down.

Known low/theoretical residuals (recorded, non-blocking per review)

  • Comparator transitivity when a third non-matching phase-0 exit shares the bar with the matched pair (not reproduced; optional hardening noted).
  • close_entries_rule="ANY" percent-partial + same-bar add coverage unverified vs TV.
  • Gapped-scratch pricing under nonzero slippage untested (all pinned data ran slippage=0).

🤖 Generated with Claude Code

When a pre-queued same-ID market add and its from_entry bracket exit both
fill on the same bar, TradingView covers the add (a dur-0 scratch) whenever
the add fills at-or-before the exit in the fill sequence; an exit that fills
first is sized to the pre-add position and the add survives to carry.

Probe-pinned rule (pf-probe-ki62-keepscratch-{gapped,intrabar}, 9,504/9,504
events; masayanfx reconstruction 4,190/4,190 including both exemplars):
- Intrabar exit fills (open inside the bracket) always cover the open-filled
  add, scratching it at the bracket price.
- At the open tick, fill priority is buy market-like (market + triggered
  stops), then sell market-like, then gapped-through limit orders. The only
  keep cell is a short position whose open gaps at-or-through the buy stop.

Implementation:
- sort_orders_by_fill_phase: the blanket exit-before-same-dir-add tie-break
  becomes the three-tier open-tick priority, scoped to a market add paired
  with its own gapped from_entry bracket; all other pairs keep the old order.
- apply_exit_order_fill: after the frozen reserve drains pre-add slices,
  any same-bar market-add slice of the same from_entry still open is
  scratched dur-0 at the exit's fill price (cover_samebar_market_adds_on_exit;
  PyramidEntry.market_pyramid_add flags the two market-add sites and is
  propagated through fifo_drain). Prior-bar slices are never touched.

Gates: new suite 30/30 (21 RED on base); ctest 95/95; corpus 240/11/1;
masayanfx 10,105/10,105 (100% px, canonical P90s 0); keep/scratch probes
9,504/9,504 both; lukeborgerding, 3commas-bch-heikin, waranyutrkm pair,
ahtisham, ki65/coof probes and coof cohort byte-identical; full 412-slug
sweep 1 up / 0 down.

Known residuals (recorded, low/theoretical): comparator transitivity when a
third non-matching phase-0 exit shares the bar; ANY-mode percent-partial +
same-bar add coverage unverified vs TV; gapped-scratch pricing under nonzero
slippage untested (all pinned data ran slippage=0).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@luisleo526 luisleo526 merged commit 81d6801 into main Jul 11, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant